package com.moyou.weichat.mvp.presenter;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTabHost;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.moyou.weichat.MainActivity;
import com.moyou.weichat.R;
import com.moyou.weichat.chat.ChatManger;
import com.moyou.weichat.fragment.ChatFragment;
import com.moyou.weichat.fragment.FindFragment;
import com.moyou.weichat.fragment.LiveFragment;
import com.moyou.weichat.fragment.UserFragment;
import com.moyou.weichat.mvp.BasePresenter;
import com.moyou.weichat.mvp.view.BaseView;
import com.moyou.weichat.mvp.view.MainView;
import com.moyou.weichat.utils.T;

/**
 * Created by Administrator on 2017/10/16 0016.
 */

public class MainPresenter extends BasePresenter<MainView> {
    private View indicator, viewTab;
    private TextView tvMassageNumber;

    public MainPresenter(MainView view) {
        super(view);
    }

    //初始化TabHost
    public void initTabView(MainActivity context,FragmentTabHost tabHost ) {
        FragmentManager supportFragmentManager = context.getSupportFragmentManager();
        tabHost.setup(context, supportFragmentManager, R.id.main_real_tab);
        tabHost.getTabWidget().setDividerDrawable(null);
        tabHost.getTabWidget().setMinimumHeight(context.getResources().getDimensionPixelOffset(R.dimen.tab_height));
    }
    //初始化Fragment
    public void initFragment(FragmentTabHost tabHost,Context context) {
        setFragment(context,"dynamic", "首页", R.drawable.tab_dynamic, LiveFragment.class, 1,tabHost);
        setFragment(context,"chat", "消息", R.drawable.tab_chat, ChatFragment.class, 2,tabHost);
        setFragment(context,"find", "发现", R.drawable.tab_find, FindFragment.class, 4,tabHost);
        setFragment(context,"user", "我的", R.drawable.tab_nearby, UserFragment.class, 5,tabHost);
    }
    //填充fragment
    private void setFragment(Context context ,String tag, String name, int selector, Class fragment, int number ,FragmentTabHost tabHost) {
        indicator = getIndicatorView(name, context.getResources().getDrawable(selector), number,context);
        tabHost.addTab(tabHost.newTabSpec(tag).setIndicator(indicator), fragment, null);
    }
    //设置View
    private View getIndicatorView(String name, Drawable drawable, int i,Context context) {
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        viewTab = View.inflate(context,R.layout.tab_main_item, null);
        TextView text = (TextView) viewTab.findViewById(R.id.tab_text);
        if (name.equals("消息")) {
            tvMassageNumber = (TextView) viewTab.findViewById(R.id.unread_address_number);
            view.setChangeMessageNbumber(tvMassageNumber , ChatManger.getChatManger().getUnreadMsgCountTotal());
        }
        text.setCompoundDrawables(null, drawable, null, null);
        text.setText(name);
        return viewTab;
    }

    //更改地步的消息未读的数量
    public void setMessageNumber(int messageNumber){
        if (tvMassageNumber == null)
            return;
            view.setChangeMessageNbumber(tvMassageNumber ,messageNumber);
    }
}
